{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5fd598c6",
   "metadata": {},
   "source": [
    "<center><font size=6>Nonlionear Systems of Equations</font></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d09f6b60",
   "metadata": {},
   "source": [
    "## Step #1 问题的定义"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d19f1b48",
   "metadata": {},
   "source": [
    "### 求解的方程组：\n",
    "$$\n",
    "{x_1}^2+{x_1}{x_2}=6\\\\\n",
    "{x_1}+{x_2}^2=5\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bea7146d",
   "metadata": {},
   "source": [
    "### 对应函数：\n",
    "$$\n",
    "f_1(x_1,x_2)={x_1}^2+{x_1}{x_2}-6\\\\\n",
    "f_2(x_1,x_2)={x_1}+{x_2}^2-5\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6231d80",
   "metadata": {},
   "source": [
    "### 写出F（x）\n",
    "$$\n",
    "F(X)=\\begin{bmatrix}\n",
    "{x_1}^2+{x_1}{x_2}-6\\\\\n",
    "{x_1}+{x_2}^2-5\\\\\n",
    "\\end{bmatrix}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3bf1e366",
   "metadata": {},
   "source": [
    "### 写出DF（x）\n",
    "$$\n",
    "DF(X)=\\begin{bmatrix}\n",
    "2{x_1}+{x_2}&{x_1}\\\\\n",
    "1&4{x_2}\\\\\n",
    "\\end{bmatrix}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7e9b16a",
   "metadata": {},
   "source": [
    "## Step #2 写出F(X)的Python代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5f058a76",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fbb4f1ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "def F(X):\n",
    "    f1=X[0]**2+X[0]*X[1]-6\n",
    "    f2=X[0]+X[1]**2-5\n",
    "    return np.array([f1,f2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c45959ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-6, -5])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X=np.array([0,0])\n",
    "F(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a414d06",
   "metadata": {},
   "source": [
    "## Step #3   DF(X)的代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d9930f10",
   "metadata": {},
   "outputs": [],
   "source": [
    "def DF(X):\n",
    "    df11=2*X[0]+X[1]\n",
    "    df12=X[0]\n",
    "    df21=1\n",
    "    df22=2*X[1]\n",
    "    return np.array([[df11,df12],\n",
    "                    [df21,df22]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "93f0b16f",
   "metadata": {},
   "outputs": [],
   "source": [
    "X=np.array([1,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5544aeeb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 1],\n",
       "       [1, 2]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DF(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5767e55",
   "metadata": {},
   "source": [
    "## Step #4 写出迭代函数的Python代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "8a425ee7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calXnew(Xold):\n",
    "    s=np.linalg.inv(DF(Xold))@F(Xold)\n",
    "    Xnew=Xold-s\n",
    "    return Xnew"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1f9ff5a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2., 2.])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X=np.array([1,1])\n",
    "calXnew(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e22f8176",
   "metadata": {},
   "source": [
    "## Step #5 进行迭代计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6b353677",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n",
      "第 1 次迭代 \tXnew [2. 2.]\n",
      "0.05371900826446281\n",
      "第 2 次迭代 \tXnew [1.72727273 1.81818182]\n",
      "0.00025876976928385277\n",
      "第 3 次迭代 \tXnew [1.70470695 1.8152965 ]\n",
      "7.667519969352456e-09\n"
     ]
    }
   ],
   "source": [
    "Xold=np.array([1,1])\n",
    "for i in range(1,11):\n",
    "    Xnew=calXnew(Xold)\n",
    "    stdError=np.square(Xnew-Xold).mean()\n",
    "    print(stdError)\n",
    "    if stdError<0.00001:\n",
    "        break\n",
    "    Xold=Xnew\n",
    "    print(\"第\",i,\"次迭代\",\"\\tXnew\",Xnew)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7377adde",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
